<template>
  <div class="coupon-container" :style="{background: `url('${bg}')`}">
    <div>
    <!-- 标签页 -->
    <div class="tabs">
      <div 
        class="tab-item" 
        :class="{ active: activeTab === 'unused' }" 
        @click="handleChange('unused')"
      >
        未使用({{ unusedCoupons.length }})
      </div>
      <div 
        class="tab-item" 
        :class="{ active: activeTab === 'used' }" 
        @click="handleChange('used')"
      >
        已使用
      </div>
      <div class="tab-line" :style="tabLineStyle"></div>
    </div>

    <!-- 卡券列表 -->
    <div class="coupon-list">
      <template v-if="activeTab === 'unused'">
        <div class="coupon-item" :style="{ backgroundImage: `url(${coupon.isapply == 0 ? couponImgUrl : couponUsedImgUrl})` }" v-for="(coupon, index) in unusedCoupons" :key="index">
          <div class="coupon-left">
            <div class="coupon-price">
              <span class="currency">¥</span>
              <span class="amount">{{ coupon.ticketsum }}</span>
            </div>
          </div>
          <div class="coupon-middle">
            <div class="coupon-type">{{ coupon.ticketName }}</div>
            <div class="coupon-desc">{{ coupon.notes }}</div>
            <div class="coupon-date">{{ coupon.validTime }}</div>
          </div>
          <div class="coupon-right" @click="handleTo(coupon)">
            <div class="use-btn" v-if="coupon.isapply == 0">立即<br>使用</div>
            <div class="use-btn" v-if="coupon.isapply == 2">已过期</div>
            <div class="corner-tag" v-if="coupon.isapply == 0" :style="{ backgroundImage: `url(${dueImgUrl})`}"></div>
          </div>
        </div>
      </template>
      <template v-else>
        <div class="coupon-item used" :style="{ backgroundImage: `url(${couponUsedImgUrl})` }" v-for="(coupon, index) in usedCoupons" :key="index">
          <div class="coupon-left">
            <div class="coupon-price">
              <span class="currency">¥</span>
              <span class="amount">{{ coupon.ticketsum }}</span>
            </div>
          </div>
          <div class="coupon-middle">
            <div class="coupon-type">{{ coupon.ticketName }}</div>
            <div class="coupon-desc">{{ coupon.notes }}</div>
            <div class="coupon-date">{{ coupon.validTime }}</div>
          </div>
          <div class="coupon-right" @click="handleTo(coupon)">
            <img src="@/assets/img/used.png" alt="" style="width: 100%;">
          </div>
        </div>
      </template>
    </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted, computed } from 'vue'
import { useRouter } from 'vue-router'
import { couponApi } from '@/api'
import couponImgUrl from '@/assets/img/coupon.png'
import couponUsedImgUrl from '@/assets/img/coupon-used.png'
import dueImgUrl from '@/assets/img/due.png'

const unusedCoupons = ref([])
const usedCoupons = ref([])
const activeTab = ref('unused')
const router = useRouter()
const tabLineStyle = computed(() => {
  const activeIndex = activeTab.value === 'unused' ? 0 : 1
  return {
    transform: `translateX(${activeIndex * 170}%)`
  }
})
const fetchList = () => {
  const uid = localStorage.getItem('uid')
  couponApi.getCouponList({
    uid: uid,
    useType: activeTab.value == 'unused' ? 0 : 1
  }).then(res => {
    if(activeTab.value === 'unused') {
        unusedCoupons.value = res.data

    }else {
        usedCoupons.value = res.data
    }
  })
}
onMounted(() => {
  fetchList()
})

const handleChange = (tab) => {
  activeTab.value = tab
  fetchList()
}

const handleTo = (coupon) => {
  if(coupon.isapply == 2) {
    return false
  }
  router.push({
    path: `/coupon-detail/${coupon.id}`,
    query: {
      id: coupon.id
    }
  })
}
</script>

<style lang="less" scoped>
.coupon-container {
  min-height: 100vh;
  background-color: #f1f3f4;
  background-repeat: no-repeat;
  background-size: 100%;
}

.tabs {
  display: flex;
  background-color: white;
  position: relative;
  
  .tab-item {
    flex: 1;
    text-align: center;
    padding: 15px 0;
    font-size: 16px;
    color: #666;
    position: relative;
    
    &.active {
      color: #f15a4a;
      font-weight: bold;
    }
  }
  
  .tab-line {
    position: absolute;
    bottom: 0;
    left: 10%;
    width: 30%;
    height: 2px;
    background-color: #f15a4a;
    transition: transform 0.3s;
  }
}

.coupon-list {
  padding: 10px;
  background-color: #fff;
}

.coupon-item {
  display: flex;
  margin-bottom: 15px;
  border-radius: 8px;
  height: 100px;
  overflow: hidden;
  position: relative;
  box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
  background-size: cover;
  background-repeat: no-repeat;
  background-position: center;
  
  &.used {
    opacity: 0.7;
  }
  
  .coupon-left {
    width: 90px;
    color: white;
    display: flex;
    justify-content: center;
    align-items: center;
    position: relative;
    
   
    .coupon-price {
      display: flex;
      align-items: center;
      text-align: center;
      
      .currency {
        font-size: 18px;
        vertical-align: top;
      }
      
      .amount {
        font-size: 28px;
        font-weight: bold;
      }
    }
  }
  
  .coupon-middle {
    margin-left: 10px;
    flex: 1;
    display: flex;
    flex-direction: column;
    justify-content: center;
    position: relative; 
    
    .coupon-type {
      font-size: 16px;
      font-weight: bold;
      color: #333;
      margin-bottom: 5px;
    }
    
    .coupon-desc {
      font-size: 12px;
      color: #666;
      margin-bottom: 10px;
      // 控制在两行
      display: -webkit-box;
      -webkit-line-clamp: 2;
      -webkit-box-orient: vertical;
      overflow: hidden;
    }
    
    .coupon-date {
      font-size: 12px;
      color: #999;
    }
  }
  
  .coupon-right {
    width: 70px;
    display: flex;
    justify-content: center;
    align-items: center;
    
    .use-btn {
      color: #f15a4a;
      font-size: 14px;
      text-align: center;
      line-height: 1.2;
    }
    
    .used-tag {
      color: #999;
      font-size: 14px;
      transform: rotate(-90deg);
    }
    
    .corner-tag {
      position: absolute;
      top: 0;
      right: 0;
      background-size: contain;
      width: 50px;
      height: 50px;
    }
  }
}
</style>